import grails.util.GrailsUtil;

import org.hr.organisation.DummyUser;
import org.hr.organisation.OrganisationService;
import org.hr.xtime.absence.DayType;
import org.hr.xtime.absence.Day
import org.hr.xtime.user.Account;
import org.hr.xtime.user.Employee;

class BootStrap {
	
	OrganisationService organisationService
	
    def init = { servletContext ->
		switch(GrailsUtil.environment){
			case "development":
			  println "| Development Mode (Start Up)"
			  if(Employee.count == 0){
				  println "| test data will be generated"
				  organisationService.generateDummyOrgStructure(3, 4, null)
				  generateTestData(DummyUser.findByBoss(null))
				  generateDays();
			  }
			 
			  break
			case "test":
			  println "| Test Mode (Start Up)"
			  organisationService.generateDummyOrgStructure(3, 4, null)
			  generateTestData(DummyUser.findByBoss(null))
			  break
			case "production":
			  println "| Production Mode (Start Up)"
			  break
		  }
	}
    def destroy = {
    }
	
	def generateTestData(DummyUser user){
		Employee employee = new Employee([
			userId:user.userId,
			standIn:user.boss == null ? user.userId : user.boss.userId
			]).save(failOnError:true)
		Account account = new Account([
				employee:employee,
				status:"default",
				percentOfFTE:100,
				vacationDaysPerYear:30,
				vacationDaysFromLastYear:5,
				hoursPerDay_1:0,
				hoursPerDay_2:8,
				hoursPerDay_3:8,
				hoursPerDay_4:8,
				hoursPerDay_5:8,
				hoursPerDay_6:7,
				hoursPerDay_7:0
			]).save()
		organisationService.getTeam(user.userId)?.each{nextUser ->
			generateTestData(DummyUser.findByUserId(nextUser.userId))
		}
	}
	
	def generateDays(){
		DayType urlaub = new DayType([label:"Urlaub",
				isAbsence:true,
				approvalRequired:true,
				key:"vacation"		
			]).save()
		DayType krank = new DayType([label:"Krankheit",
				isAbsence:true,
				approvalRequired:false,
				key:"sick"
			]).save()
		DayType gleitTag = new DayType([label:"Gleittag",
				isAbsence:true,
				approvalRequired:true,
				key:"compensation"
			]).save()
		DayType fortbildung = new DayType([label:"Fortbildung",
				isAbsence:true,
				approvalRequired:true,
				key:"training"
			]).save()
		

		Calendar now = new Date().toCalendar()
		Date beginDate = new GregorianCalendar(now.get(Calendar.YEAR),1,1).getTime()

		Random random = new Random()
		int numberOfVacations
		int month
		int dayOfMonth
		String status
		Date dayDate
		GregorianCalendar c
		Employee.list().each{employee ->
			
			// generate accounts
			
			
			// generate Days
			numberOfVacations = random.nextInt(30)
			numberOfVacations.times {it->
				month = random.nextInt(12)
				dayOfMonth = random.nextInt(28)
				c = new GregorianCalendar(now.get(Calendar.YEAR),month,dayOfMonth)
				if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
					c = new GregorianCalendar(now.get(Calendar.YEAR),month,(dayOfMonth+1)%28)
				}else if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
					c = new GregorianCalendar(now.get(Calendar.YEAR),month,(dayOfMonth-1)%28)
				}
				if(c.after(now)){
					status="proposed"
				}else{ 
					status="final"
				}	
				dayDate = c.getTime()
				
				new Day(
					date:dayDate,
					type:urlaub,
					employee:employee,
					hours:8.0,
					status:status).save(failOnError:false)
				
			}
		}
	}
}
